package com.beyond.trace.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.beyond.trace.domain.TraceBase;
import com.beyond.trace.service.ITraceBaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 基地信息Controller
 * 
 * @author yao
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/trace/base")
@Api(value = "基地信息", tags = "基地信息")
public class TraceBaseController extends BaseController
{
    @Autowired
    private ITraceBaseService traceBaseService;

    //@PreAuthorize("@ss.hasPermi('beyond:base:list')")
    @GetMapping("/list")
    @ApiOperation("查询基地信息列表")
    public TableDataInfo list(TraceBase traceBase)
    {
        startPage();
        List<TraceBase> list = traceBaseService.selectTraceBaseList(traceBase);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('beyond:base:query')")
    @GetMapping(value = "/query")
    @ApiOperation("查询基地信息列表不分页")
    public AjaxResult getInfoQuery(TraceBase traceBase)
    {
        return AjaxResult.success(traceBaseService.selectTraceBaseList(traceBase));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:base:export')")
    @Log(title = "基地信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TraceBase traceBase)
    {
        List<TraceBase> list = traceBaseService.selectTraceBaseList(traceBase);
        ExcelUtil<TraceBase> util = new ExcelUtil<TraceBase>(TraceBase.class);
        return util.exportExcel(list, "基地信息数据");
    }

    //@PreAuthorize("@ss.hasPermi('beyond:base:query')")
    @GetMapping(value = "/{baseId}")
    @ApiOperation("查询基地信息列表")
    public AjaxResult getInfo(@PathVariable("baseId") Long baseId)
    {
        return AjaxResult.success(traceBaseService.selectTraceBaseById(baseId));
    }

    @GetMapping(value = "/source")
    @ApiOperation("根据产品ID查询基地信息（原料溯源）")
    public AjaxResult getProductId(Long productId, HttpServletRequest request)
    {
        return AjaxResult.success(traceBaseService.selectTraceBaseByProductId(productId,request));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:base:add')")
    @Log(title = "基地信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增基地信息")
    public AjaxResult add(@RequestBody TraceBase traceBase)
    {
        return toAjax(traceBaseService.insertTraceBase(traceBase));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:base:edit')")
    @Log(title = "基地信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改基地信息")
    public AjaxResult edit(@RequestBody TraceBase traceBase)
    {
        int i = traceBaseService.updateTraceBase(traceBase);
        return toAjax(i);
    }

    //@PreAuthorize("@ss.hasPermi('beyond:base:remove')")
    @Log(title = "基地信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    @ApiOperation("删除基地信息")
    public AjaxResult remove(@PathVariable Long[] baseIds)
    {
        return toAjax(traceBaseService.deleteTraceBaseByIds(baseIds));
    }

    @Log(title = "门店图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/picture/upload")
    @ApiOperation("门店图片上传接口")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            if (!file.isEmpty()) {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/base", file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("path", avatar);
                return ajax;
            } else {
                return AjaxResult.error("上传图片异常，请联系管理员");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping(value = "/addScanCount")
    @ApiOperation("根据产品ID添加扫描次数")
    public AjaxResult addScanCount(Long productId, HttpServletRequest request)
    {
        return AjaxResult.success(traceBaseService.addScanCount(productId,request));
    }

}
